﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogEngineMVC.Models;

namespace BlogEngineMVC.Controllers
{
    public class HomeController : Controller
    {
        private BlogPostListDataContext db = new BlogPostListDataContext();
        public ActionResult Index()
        {
            var posts = from p in db.BlogPosts orderby p.CreateDate descending select p;
            ViewData["Title"] = "BlogEngineMVC";
            ViewData["Message"] = "Homepage for BlogEngineMVC";
            return View(posts.ToList());
        }

        public ActionResult About()
        {
            ViewData["Title"] = "About Page";

            return View();
        }
        public ActionResult Create()
        {
            return View();
        }
        public ActionResult CreateNew(string title,string body)
        {
            BlogPost post = new BlogPost();
            post.Title = title;
            post.Body = body;
            post.CreateDate = DateTime.Now;
            db.BlogPosts.InsertOnSubmit(post);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }
    }
}
